#
# Copyright 2024 Kumo.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###########################################################################
# below are examples, after initialization, modify it to your own.
###########################################################################
###kmcmake_example

bison_target(
        sql_grammar ${CMAKE_CURRENT_SOURCE_DIR}/parser/grammar/grammar.y ${CMAKE_CURRENT_SOURCE_DIR}/parser/grammar/grammar.cc
        DEFINES_FILE ${CMAKE_CURRENT_SOURCE_DIR}/parser/grammar/grammar.h
        COMPILE_FLAGS "")

flex_target(
        sql_scanner ${CMAKE_CURRENT_SOURCE_DIR}/parser/grammar/scanner.l ${CMAKE_CURRENT_SOURCE_DIR}/parser/grammar/scanner.cc
        DEFINES_FILE ${CMAKE_CURRENT_SOURCE_DIR}/parser/grammar/scanner.h
        COMPILE_FLAGS "")

add_flex_bison_dependency(sql_scanner sql_grammar)

set(PROTO_FILES
        proto/parser/ast_node_type.proto
        proto/parser/ast.proto
        proto/parser/ast_enum.proto
        proto/parser/dcl.proto
        proto/common.proto
        proto/resolved.proto
        proto/type.proto
        proto/catalog.proto
        proto/function.proto
        proto/value.proto
        proto/enums/statement_type.proto
        proto/enums/expression.proto

)

kmcmake_cc_proto_target(
        NAME proto_obj
        PROTOS
        ${PROTO_FILES}
        OUTDIR
        ${PROJECT_SOURCE_DIR}
)

kmcmake_cc_object(
        NAMESPACE cantor
        NAME proto_obj
        SOURCES
        ${proto_obj_SRCS}
        CXXOPTS
        ${KMCMAKE_CXX_OPTIONS}
)

##########################################################################
# transform parser
##########################################################################

set(PARSER_SRC
        common/expr_class.cc
        common/log.cc
        parser/ast/base.cc
        parser/ast/clause.cc
        parser/ast/expr.cc
        parser/ast/func_ctor.cc
        parser/ast/func_expr.cc
        parser/ast/func_when_clause.cc
        parser/ast/dcl.cc
        parser/ast/ddl.cc
        parser/ast/dml.cc
        parser/ast/dql.cc
        parser/ast/stmt.cc
        parser/ast/type_name.cc
        parser/parser.cc
        parser/flags.cc
        ${BISON_sql_grammar_OUTPUTS}
        ${FLEX_sql_scanner_OUTPUTS}
)

##########################################################################
# catalog sources
##########################################################################
set(CATALOG_SRC
        catalog/factory/namespace_info.cc
        catalog/namespace_manager.cc
        catalog/namespace_querier.cc
        catalog/database_manager.cc
        catalog/database_querier.cc
        catalog/table_manager.cc
        catalog/table_querier.cc
)
##########################################################################
# transform sources
##########################################################################
file(GLOB_RECURSE TRANS_SRC "transform/*.cc")

##########################################################################
# unresolved sources
##########################################################################
file(GLOB_RECURSE UNRESOLVED_SRC "unresolved/*.cc")

##########################################################################
# resolved sources
##########################################################################
set(RESOLVE_SRC
        resolved/pass_manager.cc
        resolved/stmt.cc
        resolved/stmts/start_txn_stmt.cc
        resolved/stmts/commit_txn_stmt.cc
        resolved/stmts/rollback_txn_stmt.cc
        resolved/stmts/type_stmt.cc
        function/signature.cc
        function/registry.cc
        function/builtin.cc
        function/type_traits.cc
        function/scalar_function.cc
        function/resolver.cc
        function/builtins/add.cc
        function/maths/maths.cc
        function/maths/abs.cc
)
set(UNRESOLVE_SRC
        ${PARSER_SRC}
        ${TRANS_SRC}
        ${UNRESOLVED_SRC}
        ${RESOLVE_SRC}
        ${CATALOG_SRC}
)

kmcmake_cc_library(
        NAMESPACE cantor
        NAME parser
        SOURCES
        type/type.cc
        type/type_kind.cc
        type/enum_builder.cc
        type/registry.cc
        type/signature.cc
        type/udf_registry.cc
        type/promote.cc
        type/value.cc
        utility/json_reader.cc
        ${UNRESOLVE_SRC}
        OBJECTS
        proto_obj
        DEPS
        proto_obj
        CXXOPTS
        ${KMCMAKE_CXX_OPTIONS}
        PUBLIC
)


###kmcmake_example